package com.amqp.send;

import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;

import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.concurrent.TimeoutException;

/**
 * @Author: sijunqiang
 * @Date: 2020/6/12 13:28
 * @Description:
 */
public class SendMessage {

    private final static String QUEUE_NAME = "queue_name";

    public static void main(String[] args) throws IOException, TimeoutException {
        // 创建rabbitmq的连接工厂
        ConnectionFactory factory = new ConnectionFactory();
        //设置rabbitmq节点地址
        factory.setHost("localhost");
        factory.setPort(8080);

        try (  // 开始连接
               Connection connection = factory.newConnection();
               // 创建连接通道
               Channel channel = connection.createChannel();) {

            // 声明一个队列  参数1 队列的名字 参数2 队列是否持久化 参数3 队列是否排他性就是说队列是否只能存在一个消费者， 参数4 队列其他的参数
            channel.queueDeclare(QUEUE_NAME, false, false, false, null);
            //定义一个要发送的消息
            String message = "hello rabbitmq I came ";
            // 发送消息到队列中
            channel.basicPublish("", QUEUE_NAME, null, message.getBytes(StandardCharsets.UTF_8));
            System.out.println(" [x] SendMessage '" + message + "'");
        }
    }
}
